# -*- coding: utf-8 -*-
# @Author : gongyu
# @Date   : 2019/4/4
# @File   : EC01_CreditBusinessLoanOrgCnt.py
# @E-mail : gongyu@bbdservice.com

'''eof
name:报告期内信贷业务机构数量
code:EC01_CreditBusinessLoanOrgCnt_2D
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ_2D
type:常用指标
dataSourceType:
eof'''

import sys
reload(sys)
sys.setdefaultencoding('utf-8')


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif table == "":
            return ""
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)

    if occurTimes == 1:
        if table == "":
            return ""
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def EC01_CreditBusinessLoanOrgCnt():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        data = EC_DSJ_2D['data']
        table1=getTable(data,"EBA",1)
        if table1 is None or table1 == "":
            return u"缺失值"
        elif table1==[{}]:
            return u"缺失值"
        else:
            table2=getTable(table1[0],"EB01",0)
            if table2 is None or table2 == "":
                return u"缺失值"
            elif table2 ==[{}]:
                return u"缺失值"
            else:
                table3=getTable(table2[0],"EB01A",0)
                if table3 is None:
                    return u'缺失值'
                elif table3 == [{}] or table3 == "":
                    return u"缺失值"
                else:
                    #报告期内信贷业务机构数量
                    if table3[0].has_key('EB01AS01') and table3[0]['EB01AS01'] not in null_type_list:
                        if isfloat(table3[0]['EB01AS01'])==True:
                            return float(table3[0]['EB01AS01'])
                        else:
                            return u"缺失值"
                    else:
                        return u"缺失值"
    except:
        return u'缺失值'
if __name__ == "__main__":
    import json
    with open(r'C:\Users\zdr\Desktop\samples\EC_DSJ_2D.json') as ff:
        EC_DSJ_2D = json.loads(ff.read())
    aa = EC01_CreditBusinessLoanOrgCnt()
    print(aa)
result = EC01_CreditBusinessLoanOrgCnt()
